<?php
	/**
	 * 救济金列表
	 */
    class  AlmsList extends APIBase {
		
		public $tag = "AlmsList";
		public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}

		public function logic() {
			$uid = $this->uid;
			$chance = array();
			$ID = array();

			
			//获取用户领取救济金的次数
			//$benefit_cnt = $this->data_redis->hGet("hu:{$uid}","benefit_cnt");
			$almslist = $this->cache_redis->keys("halmsreward:*");
			$R = mt_rand(1,100);//产生一个随机数
			$interval = array();//中奖区间
			
			for($i=1;$i<=count($almslist);$i++){
				$list = $this->cache_redis->hGetAll("halmsreward:".$i);
				$ID[] = $list['id'];
				$chance[] = $list['chance'];
			}
			sort($chance);
			$min = 0;
			$max = 100;
			
			//生成中奖区间
			for($i=0;$i<count($chance);$i++){
				if($i==0){
					$max = $min+$chance[$i];
					$interval[] = array($min,$max,$ID[$i]);
				}else{
					$min = $max;
					$max = $min+$chance[$i];
					$interval[] = array($min,$max,$ID[$i]);
				}
			}
			
            for($i=0;$i<count($interval);$i++){
				if($R>$interval[$i][0] && $R<=$interval[$i][1]){
					$rewardlist = $this->cache_redis->hGetAll("halmsreward:".$interval[$i][2]);
					$alms['alms'] = array((int)$rewardlist['rewardone'],(int)$rewardlist['rewardtwo'],(int)$rewardlist['rewardthree']);
					shuffle($alms['alms']);
					$alms['num'] = (int)$alms['alms'][rand(0,2)];
					
					//取出用户金币与领取救济金次数
					$uinfo = $this->data_redis->hMGet("hu:".$uid,array('money','benefit_cnt'));
					$total = (int)$uinfo['money']+(int)$alms['num'];
					$benefit_cnt = $uinfo['benefit_cnt'];
					if($benefit_cnt < Game::$broke_num_max){
				        $benefit_cnt = $benefit_cnt+1;
						$this->data_redis->hMSet("hu:".$uid,array('money'=>$total,'benefit_cnt'=>$benefit_cnt));
						$logdata =  array(
				                    'type'=>6,
				                    'vid' => 0,
				                    'tid'=>  0,
				                    'seatid'=> 0,
				                    'uid' => $uid,
				                    'change_money' =>(int)$alms['num'],
									'money'=>(int)$total,
				                    'create_time' => time(),
			                    );
                        $this->addChangeMongeyLog($logdata);
						$this->returnData($alms);
			        }else{
						$this->returnError(301,'你已经领完今日救济金次数！');
					}
					
				}
			}
		}
		
    	public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
